<?php
namespace app\gr\controller;

use cmf\controller\AdminBaseController;
use app\gr\BLL\CardTypeBLL;

class AdminCardTypeController extends AdminBaseController
{
    private $bll;
    private $ControlName = "AdminCardType";
    private $ValidateName = "AdminCardType";

    public function __construct(CardTypeBLL $cardType)
    {
        parent::__construct();
        $this->bll = $cardType;
    }

    public function index()
    {
        $params = $this->request->param();

        $where['status'] = 1;
        $request = input('request.');

        $keywordComplex = [];
        if (!empty($request['keyword'])) {
            $keyword = $request['keyword'];

            $keywordComplex['name']    = ['like', "%$keyword%"];
            $keywordComplex['spk'] = ['like', "%$keyword%"];
        }
        
        $list=$this->bll
                ->whereOR($keywordComplex)
                ->order('id desc')
                ->findWherePage($where);
                
        $list->appends($params);
        // 获取分页显示
        $page = $list->render();

        $this->assign('list', $list);
        $this->assign('page', $page);
        // 渲染模板输出
        return $this->fetch();
    }

    public function add()
    {
        return $this->fetch();
    }

    public function addPost()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();
            $result = $this->validate($data, $this->ValidateName);
            if ($result !== true) {
                $this->error($result);
            }
            if($this->bll->create($data)){
                $this->success('新增成功', url("$this->ControlName/index"));
            }else{
                $this->error("添加失败！");
            }
        }
    }

    public function edit()
    {
        $id = $this->request->param('id', 0, 'intval');
        $card = $this->bll->getbyid($id);
        $this->assign('card',$card);
        return $this->fetch();
    }

    public function editPost()
    {
        if ($this->request->isPost()) {
            $id = $this->request->param('id', 0, 'intval');
            $data = $this->request->param();
            $result = $this->validate($data, $this->ValidateName);
            if ($result !== true) {
                $this->error($result);
            }
            if($this->bll->update($data,$id)){
                $this->success("更新成功！", url("$this->ControlName/index"));
            }else{
                $this->error('更新失败！');
            }
        }
    }

    public function delete()
    {
        $id = $this->request->param('id', 0, 'intval');
        if (!empty($id)) {
            if($this->bll->delete($id)){
                $this->success("删除成功！", url("$this->ControlName/index"));
            }else{
                $this->error('删除失败！');
            }
        } else {
            $this->error('数据传入失败！');
        }
    }
}
